<template>
  <view>
    <!-- 搜索栏 -->
    <view class="search-box">
      <my-search />
    </view>

    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular>
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <image :src="item.image_src" mode="scaleToFill" />
      </swiper-item>
    </swiper>
    <!-- 导航菜单 -->
    <view class="nav-list">
      <image
        v-for="item in catitems"
        :key="item.name"
        :src="item.image_src"
        mode="scaleToFill"
        class="nav-img"
        @click="gotoCatitem(item.name)"
      />
    </view>
    <!-- 楼层 -->
    <view v-for="item in floordata" :key="item.floor_title.name">
      <image
        class="floor-title"
        :src="item.floor_title.image_src"
        mode="scaleToFill"
      />
      <view class="floor-img-box">
        <!-- 左边的图片 -->
        <navigator
          :url="item.product_list[0].navigator_url"
          open-type="navigate"
          hover-class="navigator-hover"
        >
          <image
            :style="{ width: item.product_list[0].image_width + 'rpx' }"
            :src="item.product_list[0].image_src"
            mode="widthFix"
          />
        </navigator>
        <!-- 右边的图片 -->
        <view class="right-img-box">
          <navigator
            v-for="(subitem, index) in item.product_list"
            :key="subitem.name"
            :url="subitem.navigator_url"
            open-type="navigate"
            hover-class="navigator-hover"
          >
            <image
              v-show="index > 0"
              :style="{ width: subitem.image_width + 'rpx' }"
              :src="subitem.image_src"
              mode="widthFix"
            />
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import mySearch from '../../components/my-search/my-search.vue'
export default {
  components: { mySearch },

  data () {
    return {
      swiperList: [], // 轮播图
      catitems: [], // 分类
      floordata: [] // 楼层数据
    }
  },
  onLoad () {
    this.getSwiperList()
    this.getCatitems()
    this.getFloordata()
  },
  methods: {
    async getSwiperList () {
      const res = await uni.$request({
        url: 'home/swiperdata'
      })
      if (res.meta.status === 200) {
        this.swiperList = res.message
      }
    },
    async getCatitems () {
      const res = await uni.$request({
        url: 'home/catitems'
      })
      if (res.meta.status === 200) {
        this.catitems = res.message
      }
    },
    async getFloordata () {
      const res = await uni.$request({
        url: 'home/floordata'
      })
      if (res.meta.status === 200) {
        res.message.forEach(item => {
          item.product_list.forEach(subitem => {
            subitem.navigator_url =
              '/subpkg/goods-list/index?' + subitem.navigator_url.split('?')[1]
          })
        })
        this.floordata = res.message
      }
    },
    gotoCatitem (name) {
      if (name === '分类') {
        uni.switchTab({ url: '/pages/category/index' })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
  ::v-deep .uni-searchbar {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    position: relative;
    padding: 16rpx;
    /* 将默认的 #FFFFFF 改为 #C00000 */
    background-color: #c00000;
  }
}
swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
